﻿/*
   +----------------------------------------------------------------------+
   | TABLE EXPORT TOOL                                                    |
   +----------------------------------------------------------------------+
   | Copyright © 2013 - 2018, The Machine Studio.                         |
   +----------------------------------------------------------------------+
   | See https://gitee.com/signorinotang/ServerStudy for last version     | 
   |                                                                      |
   +----------------------------------------------------------------------+
   | Author: Downey <Email: signorinotang@sina.cn>                        |
   |                <QQ: 116297528>                                       |                                                                     |
   +----------------------------------------------------------------------+
*/

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Data;
using System.IO;

namespace tabtool
{
    class Program
    {
        static void Main(string[] args) {

            //读取josn配置文件
            var json = File.ReadAllText("config.json");
            var node = SimpleJSON.JSON.Parse(json);
            string clientOutDir = node["out_client"];
            string serverOutDir = node["out_server"];
            string cppOutDir = node["out_cpp"];
            string csOutDir = node["out_cs"];
            string cpp_em_dir = node["out_cpp_em"];
            string cs_em_dir = node["out_cs_em"];
            string excelDir = node["in_excel"];
            string metafile = node["in_tbs"];
            CodeGen.space_name = node["space_name"];
            var c = node["associats"];
            //用正则匹配出associats信息
            foreach(var info in c.Childs) {
                Match reg = null;
                List<string> asc = new List<string>();
                if ((reg = Regex.Match(info, @"(\w+).(\w+)\s=\s(\w+).(\w+)(.?)")).Success) {
                    for(int i = 1; i < reg.Groups.Count; ++i) {
                        asc.Add(reg.Groups[i].Value);
                    }  
                    CodeGen.associate_info.Add(asc);
                } else {
                    Console.WriteLine("associats 配置出错不能解析" + info);
                    Console.WriteLine("按任意键退出...");
                    Console.ReadKey(false);
                    return;
                }
            }
                     
            //创建导出目录
            if (!Directory.Exists(clientOutDir)) Directory.CreateDirectory(clientOutDir);
            if (!Directory.Exists(serverOutDir)) Directory.CreateDirectory(serverOutDir);

            //先读取tablemata文件
            TableStruct tbs = new TableStruct();
            if (!tbs.ImportTableStruct(metafile)) {
                Console.WriteLine("解析tbs文件错误！");
                Console.WriteLine("按任意键退出...");
                Console.ReadKey(false);
                return;
            }
            Console.WriteLine("+------------------+");
            Console.WriteLine("<解析tbs文件成功>");

            List<TableMeta> clientTableMetaList = new List<TableMeta>();
            List<TableMeta> serverTableMetaList = new List<TableMeta>();
            List<TableMeta> allTableMeataList = new List<TableMeta>();
            //导出文件
            ExcelHelper helper = new ExcelHelper();
            //读取数据表
            string[] files = Directory.GetFiles(excelDir, "*.xlsm", SearchOption.TopDirectoryOnly);
            foreach (string filepath in files) {
                try {
                    DataTable dt = helper.ImportExcelFile(filepath);
                    TableMeta meta = null;
                    if (helper.IsExportFile("client", dt)) {
                        meta = helper.ParseTableMeta(Path.GetFileNameWithoutExtension(filepath), dt, "client");
                        clientTableMetaList.Add(meta);
                    }
                    if (helper.IsExportFile("server", dt)) {
                        meta = helper.ParseTableMeta(Path.GetFileNameWithoutExtension(filepath), dt, "server");
                        serverTableMetaList.Add(meta);
                    }
                    if(meta != null)  allTableMeataList.Add(meta);
                }
                catch (Exception e)
                {
                    Console.WriteLine(filepath + " 文件出错！");
                    Console.WriteLine(e.ToString());
                    Console.WriteLine("解析表格文件错误！");
                    Console.WriteLine("按任意键退出...");
                    Console.ReadKey(false);
                    return;
                }
            }
            Console.WriteLine("+------------------+");
            Console.WriteLine("<解析表格文件成功>");
                 
            try {
                //1 进行所有表格数据校验
                Verifier.CheckTable(allTableMeataList, excelDir);
                Console.WriteLine("+------------------+");
                Console.WriteLine("<表格校验成功>");

                //2 导出配置文件
                //导出客户端配置
                helper.ExportAllData(clientOutDir, clientTableMetaList, "client", new int[] { 1, 2 });
                //导出服务器配置
                helper.ExportAllData(serverOutDir, serverTableMetaList, "server", new int[] { 1, 2 });
                Console.WriteLine("+------------------+");
                Console.WriteLine("<导出配置文件成功>");

                //3 生成c++代码
                if (!Directory.Exists(cppOutDir)) Directory.CreateDirectory(cppOutDir);
                CodeGen.DelectDir(cppOutDir);
                CodeGen.MakeCppEnumAndMask(excelDir, cpp_em_dir);            
                CodeGen.MakeCppFileTbs(TableStruct.GetMetaList(), cppOutDir);
                CodeGen.MakeCppFile(serverTableMetaList, cppOutDir);
                Console.WriteLine("+------------------+");
                Console.WriteLine("<生成C++代码文件成功>");    
                
                //4 生成c#代码
                if (!Directory.Exists(csOutDir)) Directory.CreateDirectory(csOutDir);
                CodeGen.DelectDir(csOutDir);
                CodeGen.MakeCsharpEnumAndMask(excelDir, cs_em_dir);
                CodeGen.MakeCsharpFileTbs(TableStruct.GetMetaList(), csOutDir);
                CodeGen.MakeCsharpFile(clientTableMetaList, csOutDir);
                Console.WriteLine("+------------------+");
                Console.WriteLine("<生成C#代码文件成功>");
                Console.WriteLine("+------------------+");
               
            } catch(Exception e) {
                Console.WriteLine(e.ToString());
            }
            Console.WriteLine("按任意键退出...");
            Console.ReadKey(false);
        }
    }
}
